package connectfour;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Insets;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.*;

public class Config implements ActionListener, ChangeListener {
	private JFrame configFrame = new JFrame("Connect4 Game Setup");

	// game mode stuff
	private JPanel gmPanel = new JPanel(); // game mode panel
	private JRadioButton cvc = new JRadioButton("AI vs AI");
	private JRadioButton pvp = new JRadioButton("Player vs Player");
	private JRadioButton pvc = new JRadioButton("Player vs AI");
	private ButtonGroup btg = new ButtonGroup();

	// ai lvel stuff
	private JPanel aiPanel = new JPanel(); // ai level panel
	private String[] opts = { "0 - Braindead Baboon", "1", "2", "3",
			"4 - Average Joe (not Bruggeman)", "5", "6", "7",
			"8 - I am Alpha and Omega" };
	private JComboBox aiLevelList = new JComboBox(opts);

	// board size stuff
	private JPanel bsPanel = new JPanel(); // board size panel
	private JSlider bSize;
	private JSlider connect;
	private JLabel bsLabel;
	private JLabel connectLabel;
	
	// other stuff
	private JButton start = new JButton("Start!");
	private int ready = -1;
	
	// config variables
	private int gt = 1;// game type - 2 = AIvAI, 1 = AIvP, 0 = PvP
	private int numBoardCols = 7;
	private int numBoardRows = 6;
	private int connectn = 4;
	private int aiLevel = 4;
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Config c = new Config();

	}
	
	public void getConfig(){
		
		while(ready == -1){
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {

			}
		}
		this.configFrame.setVisible(false);
		this.configFrame.dispose();
	}
	
	// sliders
	public void stateChanged(ChangeEvent ce){
		Object obj = ce.getSource();
		if (obj instanceof JSlider){
			JSlider s = (JSlider)obj;
			String sname = s.getName();
			if(sname.compareTo("bsize") == 0){
				this.numBoardCols = s.getValue();
				this.bsLabel.setText("Board Size: " + Integer.toString(this.numBoardCols));
				this.numBoardRows = this.numBoardCols - 1;
				this.connect.setMaximum(s.getValue());
			}
			else if(sname.compareTo("connect") == 0){
				this.connectn = s.getValue();
				this.connectLabel.setText("Connect: " + Integer.toString(this.connectn));
			}
		}
	}

	// other controls
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj instanceof JRadioButton) {
			JRadioButton butt = (JRadioButton) obj;
			String name = butt.getName();
			if(name.compareTo("cvc") == 0){
				this.gt = 2;
				this.aiLevelList.setEnabled(true);
			}
			else if(name.compareTo("pvc") == 0){
				this.gt = 1;
				this.aiLevelList.setEnabled(true);
			}
			else if(name.compareTo("pvp") == 0){
				this.gt = 0;
				this.aiLevelList.setEnabled(false);
			}
		}
		else if(obj instanceof JComboBox){
			JComboBox cb = (JComboBox)obj;
			if(cb.getName().compareTo("ailevel") == 0){
				this.aiLevel = cb.getSelectedIndex();
			}
		}
		else if(obj instanceof JButton){
			JButton jb = (JButton)obj;
			if(jb.getName().compareTo("start") == 0){
				this.ready = 1;
			}
		}
	}

	public Config() {
		// configure gmPanel stuff
		this.configGMPanel();

		// configure ai level stuff
		this.configAIPanel();

		// configure board size stuff
		this.configBSPanel();

		// configure the frame
		this.configureFrame();
	}
	
	private void configureFrame(){
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		configFrame.setLayout(gbl);

		// add game mode panel
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		configFrame.add(gmPanel, gbc);

		// add ai panel
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		configFrame.add(aiPanel, gbc);

		// add bs/connect panel
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		configFrame.add(bsPanel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.anchor = GridBagConstraints.EAST;
		gbc.fill = GridBagConstraints.NONE;
		start.addActionListener(this);
		start.setName("start");
		configFrame.add(start, gbc);

		configFrame.pack();
		configFrame.setVisible(true);
		configFrame.setResizable(false);
		// change to close window only not to close app later
		configFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	
	private void configBSPanel(){
		bsPanel.setLayout(new GridLayout(2, 2));
		Border border3 = BorderFactory
				.createTitledBorder("Board Size (x by x-1) & Connect-n");
		bsPanel.setBorder(border3);
		bSize = new JSlider(7, 10);
		bSize.setMajorTickSpacing(1);
		bSize.setSnapToTicks(true);
		bSize.setValue(7);
		bSize.setName("bsize");
		bSize.addChangeListener(this);
		connect = new JSlider(4, bSize.getValue());
		connect.setMajorTickSpacing(1);
		connect.setSnapToTicks(true);
		connect.setValue(4);
		connect.setName("connect");
		connect.addChangeListener(this);
		bsLabel = new JLabel("Board Size: " + Integer.toString(bSize.getValue()));
		connectLabel = new JLabel("Connect: " + Integer.toString(connect.getValue()));
		bsPanel.add(bSize);
		bsPanel.add(connect);
		bsPanel.add(bsLabel);
		bsPanel.add(connectLabel);	
	}
	
	private void configAIPanel(){
		aiPanel.setLayout(new GridLayout(1, 1));
		Border border2 = BorderFactory.createTitledBorder("AI Difficulty");
		aiLevelList.addActionListener(this);
		aiLevelList.setName("ailevel");
		aiPanel.setBorder(border2);
		aiPanel.add(aiLevelList);
	}
	
	private void configGMPanel(){
		cvc.addActionListener(this);
		pvc.addActionListener(this);
		pvp.addActionListener(this);		
		cvc.setName("cvc");
		pvc.setName("pvc");
		pvp.setName("pvp");
		btg.add(cvc);
		btg.add(pvc);
		btg.add(pvp); // radio button group
		btg.setSelected(pvc.getModel(), true);
		aiLevelList.setSelectedIndex(4);

		gmPanel.setLayout(new GridLayout(1, 3));
		Border border1 = BorderFactory.createTitledBorder("Game Mode");
		gmPanel.setBorder(border1);
		gmPanel.add(cvc);
		gmPanel.add(pvc);
		gmPanel.add(pvp);
	}

	public int getGameType() {
		return gt;
	}

	public int getNumBoardCols() {
		return numBoardCols;
	}

	public int getNumBoardRows() {
		return numBoardRows;
	}

	public int getConnectn() {
		return connectn;
	}

	public int getAiLevel() {
		return aiLevel;
	}

}
